﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{

    #region PROPERTIES

    private UserCredentials User
    {
        get { return Session["user"] as UserCredentials; }
        set { Session["user"] = value; }
    }

    private string SuccessMessage
    {
        get { return Session["successMessage"] as string; }
        set { Session["successMessage"] = value; }
    }


    private string ErrorMessage
    {
        get { return Session["errorMessage"] as string; }
        set { Session["errorMessage"] = value; }
    }


    #endregion 


    protected override void OnInit(EventArgs e)
    {
        string loginPage = "Default.aspx";
        string registerPage = "Register.aspx";
        // TODO :: Check if Error or Successmessage in session.

        string currentPage = Path.GetFileName(Request.PhysicalPath);
        if ((String.Equals(currentPage, loginPage, StringComparison.OrdinalIgnoreCase) == true) || (String.Equals(currentPage, registerPage, StringComparison.OrdinalIgnoreCase) == true))
        {
            navMenu.Visible = false;
        }
        if ((String.Equals(currentPage, loginPage, StringComparison.OrdinalIgnoreCase) == false) && (String.Equals(currentPage, registerPage, StringComparison.OrdinalIgnoreCase) == false))
        {
            if (Session["User"] != null)
            {
                if (Session["UserAgent"] != null)
                {
                    SessionValidation sv = new SessionValidation(Session["UserAgent"].ToString(), Request.UserAgent.ToString());

                    if (!sv.ValidateSession())
                    {
                        Session.Contents.RemoveAll();
                        Response.Redirect(ResolveUrl("~/" + loginPage));
                    }
                }
                else
                {
                    Session.Contents.RemoveAll();
                    Response.Redirect(ResolveUrl("~/" + loginPage));
                }
            }
            else
            {
                Session.Contents.RemoveAll();
                Response.Redirect(ResolveUrl("~/" + loginPage));
            }
        }
        base.OnInit(e);
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (SuccessMessage != null)
        {
            SuccesLabel.Text = SuccessMessage;
            Session["successMessage"] = null;
          
        } 
        if(ErrorMessage != null)
        {
            ErrorLabel.Text = ErrorMessage;
            Session["errorMessage"] = null;
        }
    }


}
